/*
 * Copyright (C) 1994-2021 Altair Engineering, Inc.
 * For more information, contact Altair at www.altair.com.
 *
 * This file is part of both the OpenPBS software ("OpenPBS")
 * and the PBS Professional ("PBS Pro") software.
 *
 * Open Source License Information:
 *
 * OpenPBS is free software. You can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * OpenPBS is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Commercial License Information:
 *
 * PBS Pro is commercially licensed software that shares a common core with
 * the OpenPBS software.  For a copy of the commercial license terms and
 * conditions, go to: (http://www.pbspro.com/agreement.html) or contact the
 * Altair Legal Department.
 *
 * Altair's dual-license business model allows companies, individuals, and
 * organizations to create proprietary derivative works of OpenPBS and
 * distribute them - whether embedded or bundled with other software -
 * under a commercial license agreement.
 *
 * Use of Altair's trademarks, including but not limited to "PBS™",
 * "OpenPBS®", "PBS Professional®", and "PBS Pro™" and Altair's logos is
 * subject to Altair's trademark licensing policies.
 */

#include <pbs_config.h> /* the master config generated by configure */
#include <pbs_version.h>

#include <sys/types.h>
#include <sys/time.h>
#include <errno.h>
#include <pbs_ifl.h>
#include "cmds.h"
#include "net_connect.h"

#define OPT_BUF_LEN 256

static struct attrl *attrib = NULL;
static time_t dtstart;
static time_t dtend;
int force_alter = FALSE;

/*
 * @brief process options input to the command.
 *
 * @param[in]  argc  - the number of arguments to be parsed.
 * @param[in]  argv  - the argument array.
 * @param[out] attrp - attribute list sent to the caller.
 * @param[out] dest  - destination server.
 *
 * @retval 0 on no error.
 * @retval No. of options having errors.
 */
int
process_opts(int argc, char **argv, struct attrl **attrp, char *dest)
{
	int c = 0;

	int errflg = 0;
	time_t t;

	char time_buf[80] = {0};
	char *endptr = NULL;
	long temp = 0;
	char dur_buf[800];
	int alter_duration = FALSE;

	while ((c = getopt(argc, argv, "E:I:m:M:N:R:q:U:G:D:l:W:")) != EOF) {
		switch (c) {
			case 'E':
				t = cvtdate(optarg);
				if (t >= 0) {
					(void) sprintf(time_buf, "%ld", (long) t);
					set_attr_error_exit(&attrib, ATTR_resv_end, time_buf);
					dtend = t;
				} else {
					fprintf(stderr, "pbs_ralter: illegal -E time value\n");
					errflg++;
				}
				break;

			case 'I':
				temp = strtol(optarg, &endptr, 0);
				if (*endptr == '\0' && temp > 0) {
					set_attr_error_exit(&attrib, ATTR_inter, optarg);
				} else {
					fprintf(stderr, "pbs_ralter: illegal -I time value\n");
					errflg++;
				}
				break;

			case 'm':
				set_attr_error_exit(&attrib, ATTR_m, optarg);
				break;

			case 'M':
				set_attr_error_exit(&attrib, ATTR_M, optarg);
				break;

			case 'N':
				set_attr_error_exit(&attrib, ATTR_resv_name, optarg);
				break;

			case 'R':
				t = cvtdate(optarg);
				if (t >= 0) {
					(void) sprintf(time_buf, "%ld", (long) t);
					set_attr_error_exit(&attrib, ATTR_resv_start, time_buf);
					dtstart = t;
				} else {
					fprintf(stderr, "pbs_ralter: illegal -R time value\n");
					errflg++;
				}
				break;

			case 'q':
				/* destination can only be another server */
				if (optarg[0] != '@') {
					fprintf(stderr, "pbs_ralter: illegal -q value: format \"@server\"\n");
					errflg++;
					break;
				}
				pbs_strncpy(dest, &optarg[1], OPT_BUF_LEN);
				break;

			case 'U':
				set_attr_error_exit(&attrib, ATTR_auth_u, optarg);
				break;
			case 'G':
				set_attr_error_exit(&attrib, ATTR_auth_g, optarg);
				break;

			case 'D':
				snprintf(dur_buf, sizeof(dur_buf), "%s", optarg);
				set_attr_error_exit(&attrib, ATTR_resv_duration, dur_buf);
				alter_duration = TRUE;
				break;
			case 'l':
				if (strncmp(optarg, "select=", 7) == 0)
					set_attr_resc_error_exit(&attrib, ATTR_l, "select", (optarg + 7));
				else {
					fprintf(stderr, "pbs_ralter -l only allows for select\n");
					errflg++;
				}
				break;
			case 'W':
				if (strcmp(optarg, "force") == 0)
					force_alter = TRUE;
				else {
					fprintf(stderr, "pbs_ralter: illegal -W value\n");
					errflg++;
				}
				break;
			default:
				/* pbs_ralter option not recognized */
				errflg++;

		} /* End of lengthy 'switch on option' constuction */
	}	  /* End of lengthy while loop on 'options' */

	/* Check that force option is used with 'R', 'E' or 'D' option */
	if ((force_alter == TRUE) && (dtstart == 0) && (dtend == 0) && (alter_duration == 0)) {
		fprintf(stderr, "pbs_ralter: No support for requested service\n");
		errflg++;
	}
	*attrp = attrib;
	return (errflg);
}

/*
 * @brief - prints correct usage of the command to the console.
 */

static void
print_usage()
{
	static char usag2[] = "       pbs_ralter --version\n";
	static char usage[] =
		"usage: pbs_ralter [-I seconds] [-m mail_points] [-M mail_list]\n"
		"                [-N reservation_name] [-R start_time] [-E end_time]\n"
		"                [-U (+/-)username[,(+/-)username]...]\n"
		"                [-G [(+/-)group[,(+/-)group]...]]\n"
		"                [-D duration]\n"
		"                [-W force]\n"
		"                resv_id\n";
	fprintf(stderr, "%s", usage);
	fprintf(stderr, "%s", usag2);
}

/**
 * @brief
 * 	handles attribute errors and prints appropriate errmsg
 *
 * @param[in] err_list - list of possible attribute errors
 *
 * @return - Void
 *
 */
static void
handle_attribute_errors(struct ecl_attribute_errors *err_list)
{
	struct attropl *attribute = NULL;
	char *opt = NULL;
	int i = 0;

	for (i = 0; i < err_list->ecl_numerrors; i++) {
		attribute = err_list->ecl_attrerr[i].ecl_attribute;
		if (strcmp(attribute->name, ATTR_resv_duration) == 0)
			opt = "D";
		else if (strcmp(attribute->name, ATTR_resv_end) == 0)
			opt = "E";
		else if (strcmp(attribute->name, ATTR_auth_g) == 0)
			opt = "G";
		else if (strcmp(attribute->name, ATTR_inter) == 0)
			opt = "I";
		else if (strcmp(attribute->name, ATTR_m) == 0)
			opt = "m";
		else if (strcmp(attribute->name, ATTR_M) == 0)
			opt = "M";
		else if (strcmp(attribute->name, ATTR_resv_name) == 0)
			opt = "N";
		else if (strcmp(attribute->name, ATTR_resv_start) == 0)
			opt = "R";
		else if (strcmp(attribute->name, ATTR_auth_u) == 0)
			opt = "U";
		else
			return;

		CS_close_app();
		fprintf(stderr, "pbs_ralter: illegal -%s value\n", opt);
		print_usage();
		exit(2);
	}
}

int
main(int argc, char *argv[], char *envp[]) /* pbs_ralter */
{
	int errflg = 0;			 /* command line option error */
	int connect = -1;		 /* return from pbs_connect */
	char *errmsg = NULL;		 /* return from pbs_geterrmsg */
	char destbuf[OPT_BUF_LEN] = {0}; /* buffer for option server */
	struct attrl *attrib = NULL;	 /* the attrib list */
	struct ecl_attribute_errors *err_list = NULL;
	char resv_id[PBS_MAXCLTJOBID] = {0};
	char resv_id_out[PBS_MAXCLTJOBID] = {0};
	char server_out[MAXSERVERNAME] = {0};
	char *stat = NULL;
	char *extend = NULL;

	/*test for real deal or just version and exit*/

	PRINT_VERSION_AND_EXIT(argc, argv);

	if (initsocketlib())
		return 1;

	destbuf[0] = '\0';
	errflg = process_opts(argc, argv, &attrib, destbuf); /* get cmdline options */

	if (errflg || ((optind + 1) != argc) || argc == 1) {
		print_usage();
		exit(2);
	}

	/*perform needed security library initializations (including none)*/

	if (CS_client_init() != CS_SUCCESS) {
		fprintf(stderr, "pbs_ralter: unable to initialize security library.\n");
		exit(1);
	}

	/* Connect to the server */
	connect = cnt2server(destbuf);
	if (connect <= 0) {
		fprintf(stderr, "pbs_ralter: cannot connect to server %s (errno=%d)\n",
			pbs_server, pbs_errno);
		CS_close_app();
		exit(pbs_errno);
	}

	pbs_strncpy(resv_id, argv[optind], sizeof(resv_id));
	if (get_server(resv_id, resv_id_out, server_out)) {
		fprintf(stderr, "pbs_ralter: illegally formed reservation identifier: %s\n", resv_id);
		exit(2);
	}

	if (force_alter == TRUE)
		extend = "force";
	stat = pbs_modify_resv(connect, resv_id_out, (struct attropl *) attrib, extend);

	if (stat == NULL) {
		if ((err_list = pbs_get_attributes_in_error(connect)))
			handle_attribute_errors(err_list);

		errmsg = pbs_geterrmsg(connect);
		if (errmsg != NULL) {
			fprintf(stderr, "pbs_ralter: %s\n", errmsg);
		} else
			fprintf(stderr, "pbs_ralter: Error (%d) modifying reservation\n", pbs_errno);
		CS_close_app();
		exit(pbs_errno);
	} else {
		printf("pbs_ralter: %s\n", stat);
		free(stat);
	}
	/* Disconnet from the server. */
	pbs_disconnect(connect);

	CS_close_app();
	exit(0);
}
